package com.whu.UVAProtocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description
 * @Author chengeng
 * @Date 2021/10/28 17:03
 **/
@Slf4j
public class UVAMsgEncoder extends MessageToByteEncoder<UVAMessageProtocolType1_3> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, UVAMessageProtocolType1_3 uvaMessageProtocolType1_3, ByteBuf byteBuf) throws Exception {
        log.info("正在编码数据");
        byteBuf.writeShort(uvaMessageProtocolType1_3.getMagicNumber());
//        byteBuf.writeByte(uvaMessageProtocolType1_3.getAddressProp());
        byteBuf.writeByte(uvaMessageProtocolType1_3.getControlProp());
        byteBuf.writeBytes(uvaMessageProtocolType1_3.getDivs());
        byteBuf.writeChar(uvaMessageProtocolType1_3.getSerialNum());
        byteBuf.writeShort(uvaMessageProtocolType1_3.getMsgBodyLen());
        byteBuf.writeBytes(uvaMessageProtocolType1_3.getMsgBody());
//        byteBuf.writeDouble(uvaMessageProtocolType1_3.getLongitude());
//        byteBuf.writeDouble(uvaMessageProtocolType1_3.getLatitude());
//        byteBuf.writeFloat(uvaMessageProtocolType1_3.getHeight());
//        byteBuf.writeByte(uvaMessageProtocolType1_3.getBattery());
//        byteBuf.writeBytes(uvaMessageProtocolType1_3.getTimeStamp());
        byteBuf.writeShort(uvaMessageProtocolType1_3.getCrcCheckNum());
        log.info("数据编码完成");
    }
}
